#include "formconfiguration.h"
#include "ui_formconfiguration.h"

const QString FormConfiguration::Ajouter = QString("Ajouter");
const QString FormConfiguration::Nouveau = QString("Nouveau");

FormConfiguration::FormConfiguration(databaseManager &datab, QStringList &listeTypeClient, QStringList &listeSono, ListeUsers &listeUser, ListeTypePrestation &listeTypePrestation, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormConfiguration)
{
    ui->setupUi(this);

    db = &datab;
    m_listeTypeClient = &listeTypeClient;
    m_listeTypeSono = &listeSono;
    m_listeUsers = &listeUser;
    m_listeTypePrestation = &listeTypePrestation;

    m_listeTypeClientModel = new QStringListModel();
    m_listeTypeSonoModel = new QStringListModel();
    m_listeTypePrestationModel = new QStringListModel();
    m_listeUserModel = new QStringListModel();

    m_listeTypeClientModel->setStringList(*m_listeTypeClient);
    m_listeTypeSonoModel->setStringList(*m_listeTypeSono);
    m_listeUserModel->setStringList(m_listeUsers->toStringList());
    m_listeTypePrestationModel->setStringList(m_listeTypePrestation->toStringList());

    actionSupprimerTypeClient = new QAction("Supprimer", this);
    actionSupprimerTypeClient->setShortcut(QKeySequence::Delete);
    actionSupprimerTypeSono = new QAction("Supprimer", this);
    actionSupprimerTypeSono->setShortcut(QKeySequence::Delete);
    actionSupprimerTypePrestation = new QAction("Supprimer", this);
    actionSupprimerTypePrestation->setShortcut(QKeySequence::Delete);
    actionSupprimerUser = new QAction("Supprimer", this);
    actionSupprimerUser->setShortcut(QKeySequence::Delete);

    ui->listViewTypeClient->setModel(m_listeTypeClientModel);
    ui->listViewTypeClient->setContextMenuPolicy(Qt::ActionsContextMenu);
    ui->listViewTypeClient->addAction(actionSupprimerTypeClient);
    ui->listViewTypeClient->setSelectionMode(QAbstractItemView::ContiguousSelection);

    ui->listViewSonos->setModel(m_listeTypeSonoModel);
    ui->listViewSonos->setContextMenuPolicy(Qt::ActionsContextMenu);
    ui->listViewSonos->addAction(actionSupprimerTypeSono);
    ui->comboBoxTypePrestationTypeSono->setModel(m_listeTypeSonoModel);

    ui->listViewTypePrestation->setModel(m_listeTypePrestationModel);
    ui->listViewTypePrestation->setContextMenuPolicy(Qt::ActionsContextMenu);
    ui->listViewTypePrestation->addAction(actionSupprimerTypePrestation);

    ui->listViewUtilisateurs->setModel(m_listeUserModel);
    ui->listViewUtilisateurs->setContextMenuPolicy(Qt::ActionsContextMenu);
    ui->listViewUtilisateurs->addAction(actionSupprimerUser);

    ui->pushButtonTypePrestationAjouter->setText(Ajouter);
    ui->pushButtonUtilisateursAjouter->setText(Ajouter);

    connect(ui->pushButtonSonosAjouter, SIGNAL(clicked()), this, SLOT(AjouterTypeSono()));
    connect(ui->pushButtonTypeClientAjouter, SIGNAL(clicked()), this, SLOT(AjouterTypeClient()));
    connect(ui->pushButtonTypePrestationAjouter, SIGNAL(clicked()), this, SLOT(AjouterTypePrestation()));
    connect(ui->pushButtonUtilisateursAjouter, SIGNAL(clicked()), this, SLOT(AjouterUser()));

    connect(actionSupprimerTypeClient, SIGNAL(triggered()), this, SLOT(SupprimerTypeClient()));
    connect(actionSupprimerTypeSono, SIGNAL(triggered()), this, SLOT(SupprimerTypeSono()));
    connect(actionSupprimerTypePrestation, SIGNAL(triggered()), this, SLOT(SupprimerTypePrestation()));
    connect(actionSupprimerUser, SIGNAL(triggered()), this, SLOT(SupprimerUser()));

    connect(ui->pushButtonUtilisateursModifier, SIGNAL(clicked()), SLOT(ModifierUser()));
    connect(ui->pushButtonTypePrestationModifier, SIGNAL(clicked()), SLOT(ModifierTypePrestation()));

    connect(ui->listViewTypePrestation, SIGNAL(activated(QModelIndex)), this, SLOT(SelectTypePrestation(QModelIndex)));
    connect(ui->listViewUtilisateurs, SIGNAL(activated(QModelIndex)), this, SLOT(SelectUser(QModelIndex)));

    connect(ui->lineEditTypePrestationNomPresta, SIGNAL(textEdited(QString)), this, SLOT(ResetLineEditStyleSheet(QString)));
    connect(ui->comboBoxTypePrestationTypeSono, SIGNAL(currentIndexChanged(QString)), this, SLOT(ResetLineEditStyleSheet(QString)));
    connect(ui->spinBoxTypePrestationNbDeMusiciens, SIGNAL(valueChanged(QString)), this, SLOT(ResetLineEditStyleSheet(QString)));

    currentUser = -1;
    currentTypePrestation = -1;

    ui->pushButtonTypePrestationModifier->setVisible(false);
}

FormConfiguration::~FormConfiguration()
{
    delete ui;
}
//////////////////////
//
//   Gestion data
//
//////////////////////

TypePrestation FormConfiguration::CreateTypePrestation(TypePrestation t)
{
    QString nomPresta = ui->lineEditTypePrestationNomPresta->text();
    int nbMusiciens = ui->spinBoxTypePrestationNbDeMusiciens->value();
    double tarifHT = ui->doubleSpinBoxTypePrestationTarifHT->value();
    bool isLumiere = ui->checkBoxTypePrestationIsLumiere->checkState();
    bool isDecor = ui->checkBoxTypePrestationIsDecor->checkState();
    QString typeSono = ui->comboBoxTypePrestationTypeSono->currentText();
    QString divers = ui->textEditTypePrestationDivers->toPlainText();

    t.setNomPresta(nomPresta);
    t.setNombreMusiciens(nbMusiciens);
    t.setTarifHT(tarifHT);
    t.setLumiere(isLumiere);
    t.setDecorScene(isDecor);
    t.setTypeSono(typeSono);
    t.setDivers(divers);

    return t;
}

User FormConfiguration::CreateUser(User u)
{
    Adresse a;
    a.setNumeroRue(ui->LineEditUtilisateurNumero->text());
    a.setRue(ui->LineEditUtilisateurVoie->text());
    a.setcodePostal(ui->LineEditUtilisateurCodePostal->text());
    a.setVille(ui->LineEditUtilisateurVille->text());
    u.setAdresse(a);

    Contact c;
    c.setNom(ui->lineEditNomContact->text());
    c.setPrenom(ui->lineEditPrenomContact->text());
    c.setTelephone1(ui->lineEditTel1Contact->text());
    c.setTelephone2(ui->lineEditTel2Contact->text());
    c.setAdresseMail(ui->lineEditMailContact->text());
    c.setPriorite(0);
    c.setClientId(0);
    u.setContact(c);

    return u;
}


//////////////////////
//
//   Ajouter
//
//////////////////////
void FormConfiguration::AjouterTypeClient()
{
    if (!ui->lineEditTypeClient->text().isEmpty())
    {
        if (!m_listeTypeClient->contains(ui->lineEditTypeClient->text()))
        {
            QString type = ui->lineEditTypeClient->text();
            db->InsertValue(type, ladDb.champIntitule, ladDb.NomTableTypeClient );
            *m_listeTypeClient <<  type;
            m_listeTypeClient->sort();
            m_listeTypeClientModel->setStringList(*m_listeTypeClient);
            ui->lineEditTypeClient->clear();
        }
    }

}
void FormConfiguration::AjouterTypeSono()
{
    if (!ui->lineEditSonos->text().isEmpty())
    {
        if (!m_listeTypeSono->contains(ui->lineEditSonos->text()))
        {
            QString type = ui->lineEditSonos->text();
            db->InsertValue(type, ladDb.champIntitule, ladDb.NomTableTypeSono );
            *m_listeTypeSono <<  type;
            m_listeTypeSono->sort();
            m_listeTypeSonoModel->setStringList(*m_listeTypeSono);
            ui->lineEditSonos->clear();
        }
    }
}
void FormConfiguration::AjouterTypePrestation()
{
    if (ui->pushButtonTypePrestationAjouter->text() == Ajouter)
    {
        QString nomPresta = ui->lineEditTypePrestationNomPresta->text();
        int nbMusiciens = ui->spinBoxTypePrestationNbDeMusiciens->value();
        double tarifHT = ui->doubleSpinBoxTypePrestationTarifHT->value();
        bool isLumiere = ui->checkBoxTypePrestationIsLumiere->checkState();
        bool isDecor = ui->checkBoxTypePrestationIsDecor->checkState();
        QString typeSono = ui->comboBoxTypePrestationTypeSono->currentText();
        QString divers = ui->textEditTypePrestationDivers->toPlainText();

        if (nomPresta.isEmpty())
        {
            ui->lineEditTypePrestationNomPresta->setStyleSheet("background-color: rgb(255, 0, 0);");
            return;
        }

        if (nbMusiciens == 0)
        {
            ui->spinBoxTypePrestationNbDeMusiciens->setStyleSheet("background-color: rgb(255, 0, 0);");
            return;
        }


        if (typeSono.isEmpty())
        {
            ui->comboBoxTypePrestationTypeSono->setStyleSheet("background-color: rgb(255, 0, 0);");
            return;
        }


        TypePrestation value(nomPresta, nbMusiciens, tarifHT, isLumiere, isDecor, typeSono, divers);

        if (!m_listeTypePrestation->contains(value))
        {
            int id = 0;
            id = db->InsertValue(TypePrestation::ListeChampSQL(), &value, value.SQLWhere(), ladDb.NomTableTypePrestation);
            value.setId(id);
            m_listeTypePrestation->append(value);
            Affichage();
            ClearTypePrestationFields();
        }

    }
    else if (ui->pushButtonTypePrestationAjouter->text() == Nouveau)
    {
        ClearTypePrestationFields();
        ui->pushButtonTypePrestationAjouter->setText(Ajouter);
        currentTypePrestation = -1;
        ui->pushButtonTypePrestationModifier->setEnabled(false);
    }

}
void FormConfiguration::AjouterUser()
{
    if (ui->pushButtonUtilisateursAjouter->text() == Ajouter)
    {
        if (!ui->lineEditUtilisateurNom->text().isEmpty())
        {
            QString nom = ui->lineEditUtilisateurNom->text();
            User value(nom);
            value = CreateUser(value);

            if (!m_listeUsers->contains(value))
            {
                int id = 0;
                id = db->InsertValue(User::ListeChampSQL(), &value, value.SQLWhere(), ladDb.NomTableUser);
                value.setId(id);
                m_listeUsers->append(value);
                ClearUserFields();
                Affichage();
            }
        }

    }
    else if (ui->pushButtonUtilisateursAjouter->text() == Nouveau)
    {
        ClearUserFields();
        ui->pushButtonUtilisateursAjouter->setText(Ajouter);
        currentUser = -1;
        ui->pushButtonUtilisateursModifier->setEnabled(false);
        ui->lineEditUtilisateurNom->setEnabled(true);
        ui->lineEditNomContact->setEnabled(true);
        ui->lineEditPrenomContact->setEnabled(true);
    }
}

//////////////////////
//
//   Supprimer
//
//////////////////////
void FormConfiguration::SupprimerTypeSono()
{
    QString type = ui->listViewSonos->currentIndex().data(Qt::DisplayRole).toString();
    db->RemoveValue(type, ladDb.champIntitule, ladDb.NomTableTypeSono );
    m_listeTypeSono->removeOne(type);
    m_listeTypeSonoModel->setStringList(*m_listeTypeSono);
}

void FormConfiguration::SupprimerTypeClient()
{
    QString type = ui->listViewTypeClient->currentIndex().data(Qt::DisplayRole).toString();
    db->RemoveValue(type, ladDb.champIntitule, ladDb.NomTableTypeClient );
    m_listeTypeClient->removeOne(type);
    m_listeTypeClientModel->setStringList(*m_listeTypeClient);
}
void FormConfiguration::SupprimerTypePrestation()
{
    int index = ui->listViewTypePrestation->currentIndex().row();
    db->RemoveValue(m_listeTypePrestation->at(index).SQLWhere(), ladDb.NomTableTypePrestation);
    m_listeTypePrestation->removeAt(index);
    m_listeTypePrestationModel->setStringList(m_listeTypePrestation->toStringList());
}
void FormConfiguration::SupprimerUser()
{
    int index = ui->listViewUtilisateurs->currentIndex().row();
    db->RemoveValue(m_listeUsers->at(index).SQLWhere(), ladDb.NomTableUser, true);
    m_listeUsers->removeAt(index);
    m_listeUserModel->setStringList(m_listeUsers->toStringList());
}

//////////////////////
//
//   Select
//
//////////////////////
void FormConfiguration::SelectTypePrestation(QModelIndex index)
{
    if (index.isValid())
    {
        currentTypePrestation = index.row();
        TypePrestation t;
        t = m_listeTypePrestation->at(currentTypePrestation);
        ui->lineEditTypePrestationNomPresta->setText(t.getNomPresta());
        ui->spinBoxTypePrestationNbDeMusiciens->setValue(t.getNombreMusiciens());
        ui->doubleSpinBoxTypePrestationTarifHT->setValue(t.getTarifHT());
        ui->checkBoxTypePrestationIsLumiere->setChecked(t.isLumiere());
        ui->checkBoxTypePrestationIsDecor->setChecked(t.isDecorScene());
        ui->comboBoxTypePrestationTypeSono->setCurrentIndex(m_listeTypeSono->indexOf(t.getTypeSono()));
        ui->textEditTypePrestationDivers->setText(t.getDivers());
//        ui->pushButtonTypePrestationModifier->setEnabled(true);
//        ui->pushButtonTypePrestationAjouter->setText(Nouveau);
    }
}
void FormConfiguration::SelectUser(QModelIndex index)
{
    if (index.isValid())
    {
        currentUser = index.row();
        User u;
        u = m_listeUsers->at(currentUser);
        ui->lineEditUtilisateurNom->setText(u.getNom());
        ui->lineEditUtilisateurNom->setEnabled(false);
        ui->LineEditUtilisateurNumero->setText(u.getAdresse().getNumeroRue());
        ui->LineEditUtilisateurVoie->setText(u.getAdresse().getRue());
        ui->LineEditUtilisateurCodePostal->setText(u.getAdresse().getCodePostal());
        ui->LineEditUtilisateurVille->setText(u.getAdresse().getVille());
        ui->LineEditUtilisateurDepartement->setText(u.getAdresse().getDepartement());

        ui->lineEditNomContact->setText(u.getContact().getNom());
        ui->lineEditPrenomContact->setText(u.getContact().getPrenom());
        ui->lineEditNomContact->setEnabled(false);
        ui->lineEditPrenomContact->setEnabled(false);
        ui->lineEditTel1Contact->setText(u.getContact().getTelephone1());
        ui->lineEditTel2Contact->setText(u.getContact().getTelephone2());
        ui->lineEditMailContact->setText(u.getContact().getAdresseMail());
        ui->pushButtonUtilisateursModifier->setEnabled(true);
        ui->pushButtonUtilisateursAjouter->setText(Nouveau);
    }
}

//////////////////////
//
//   Editer
//
//////////////////////
void FormConfiguration::ModifierUser()
{
    if (currentUser >= 0)
    {
        User userNew;
        userNew = m_listeUsers->at(currentUser);
        userNew = CreateUser(userNew);

        if (currentUser >= 0)
        {
            db->UpdateValue(ladDb.NomTableUser, userNew.SetQuery(), userNew.SetBindValues());
            m_listeUsers->replace(currentUser, userNew);
        }
        AjouterUser();
    }

}
void FormConfiguration::ModifierTypePrestation()
{
    if (currentTypePrestation >= 0)
    {
        TypePrestation type;
        type = m_listeTypePrestation->at(currentTypePrestation);

        QString nomPresta = ui->lineEditTypePrestationNomPresta->text();
        int nbMusiciens = ui->spinBoxTypePrestationNbDeMusiciens->value();
        QString typeSono = ui->comboBoxTypePrestationTypeSono->currentText();

        if (nomPresta.isEmpty())
        {
            ui->lineEditTypePrestationNomPresta->setStyleSheet("background-color: rgb(255, 0, 0);");
            return;
        }

        if (nbMusiciens == 0)
        {
            ui->spinBoxTypePrestationNbDeMusiciens->setStyleSheet("background-color: rgb(255, 0, 0);");
            return;
        }

        if (typeSono.isEmpty())
        {
            ui->comboBoxTypePrestationTypeSono->setStyleSheet("background-color: rgb(255, 0, 0);");
            return;
        }

        type = CreateTypePrestation(type);
        if (currentTypePrestation >= 0)
        {
    //        db.UpdateValue(typeNew.SQLSet(), NomTableTypePrestation, typeOld.SQLWhere());
//            m_listeTypePrestation->replace(currentTypePrestation, typeNew);

        }
        emit AjouterTypePrestation();
    }
}
//////////////////////
//
//   GUI
//
//////////////////////
void FormConfiguration::ResetLineEditStyleSheet(QString text)
{
    if (text == ui->lineEditTypePrestationNomPresta->text())
        ui->lineEditTypePrestationNomPresta->setStyleSheet("");

    if (text == ui->comboBoxTypePrestationTypeSono->currentText())
        ui->comboBoxTypePrestationTypeSono->setStyleSheet("");

    if (text == ui->spinBoxTypePrestationNbDeMusiciens->text())
        ui->spinBoxTypePrestationNbDeMusiciens->setStyleSheet("");
}
void FormConfiguration::ClearUserFields()
{
    ui->LineEditUtilisateurNumero->clear();
    ui->LineEditUtilisateurVoie->clear();
    ui->LineEditUtilisateurCodePostal->clear();
    ui->LineEditUtilisateurVille->clear();
    ui->LineEditUtilisateurDepartement->clear();
    ui->lineEditNomContact->clear();
    ui->lineEditPrenomContact->clear();
    ui->lineEditTel1Contact->clear();
    ui->lineEditTel2Contact->clear();
    ui->lineEditMailContact->clear();
    ui->lineEditUtilisateurNom->clear();
}
void FormConfiguration::ClearTypePrestationFields()
{
    ui->lineEditTypePrestationNomPresta->clear();
    ui->spinBoxTypePrestationNbDeMusiciens->clear();
    ui->doubleSpinBoxTypePrestationTarifHT->clear();
    ui->checkBoxTypePrestationIsLumiere->setChecked(false);
    ui->checkBoxTypePrestationIsDecor->setChecked(false);
    ui->comboBoxTypePrestationTypeSono->setCurrentIndex(0);
    ui->textEditTypePrestationDivers->clear();
}
void FormConfiguration::Affichage()
{
    m_listeTypeClientModel->setStringList(*m_listeTypeClient);
    m_listeTypeSonoModel->setStringList(*m_listeTypeSono);
    m_listeUserModel->setStringList(m_listeUsers->toStringList());
    m_listeTypePrestationModel->setStringList(m_listeTypePrestation->toStringList());

}
